package com.rxyl.love.petdoctor.acticity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.adapter.CityAdapter;
import com.rxyl.love.petdoctor.entity.CityEntity;
import com.rxyl.love.petdoctor.fragment.HospitalFragment;
import com.rxyl.love.petdoctor.fragment.MainFragment;
import com.rxyl.love.petdoctor.service.LocationService;
import com.rxyl.love.petdoctor.util.CityJSON;
import com.rxyl.love.petdoctor.util.NetworkUtil;
import com.rxyl.love.petdoctor.util.SharedUtil;
import com.rxyl.love.petdoctor.view.LabelView;
import com.rxyl.love.petdoctor.view.SideView;

import java.util.List;

public class CityActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,
        SideView.OnSelectListener {

    private ListView lvCity;
    private CityAdapter cityAdapter;
    private SideView sv;
    private LabelView lb;
    private LinearLayout ll_localCity;
    private TextView tv_localCity;
    private LocationBroadcaseRec locationBroadcaseRec;
    private Intent toService;
    private Intent toResult;
    private String latitude;
    private String longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        toResult = getIntent();
        init();
        loadData();
    }

    private void init() {
        lvCity = (ListView) findViewById(R.id.lv_city);
        cityAdapter = new CityAdapter(this);
        lvCity.setAdapter(cityAdapter);
        lvCity.setOnItemClickListener(this);
        sv = (SideView) findViewById(R.id.sv_city);
        sv.setOnSelectListener(this);
        lb = (LabelView) findViewById(R.id.lb_city);
        ll_localCity = (LinearLayout) findViewById(R.id.ll_localCity);
        tv_localCity = (TextView) findViewById(R.id.tv_localCity);
        //开始定位
        startLocation();

        ll_localCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toResult.putExtra("city", tv_localCity.getText().toString());
                toResult.putExtra("latitude",latitude);
                toResult.putExtra("longitude",longitude);
                setResult(MainFragment.RESULT_STRING, toResult);
                finish();
            }
        });
    }

    private void loadData() {
        NetworkUtil.getInstance().getCityList(new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                List<CityEntity> list = CityJSON.getCitysByJSON(responseInfo.result.toString());
                if (list != null) {
                    cityAdapter.setDatas(list);
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    private void startLocation() {
        //动态注册广播
        locationBroadcaseRec = new LocationBroadcaseRec();
        IntentFilter filter = new IntentFilter();
        filter.addAction("cityMsg");
        registerReceiver(locationBroadcaseRec, filter);

        toService = new Intent(this, LocationService.class);
        startService(toService);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CityEntity cityEntity = (CityEntity) cityAdapter.getItem(position);//获取选中的城市实体对象

        toResult.putExtra("citys", cityEntity);
        setResult(HospitalFragment.RESULT_CITY, toResult);
        finish();
    }

    @Override
    public void onSelect(String str) {
        int index = cityAdapter.eqIndex(str);
        if (index != -1) {
            lvCity.smoothScrollToPositionFromTop(index, 0);
        }
        lb.setLabel(str);
    }

    public void backTo(View view) {
        this.finish();
    }


    //广播接受定位
    class LocationBroadcaseRec extends BroadcastReceiver {


        @Override
        public void onReceive(Context context, Intent intent) {
            String cityName = intent.getStringExtra("cityName");
            latitude = intent.getStringExtra("latitude");
            longitude = intent.getStringExtra("longitude");
            SharedUtil.putString("cityname", cityName);

            tv_localCity.setText(cityName);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(locationBroadcaseRec);
    }
}
